package com.hanserwei.community.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanserwei.community.entity.common.ApiResponse;
import com.hanserwei.community.entity.po.House;
import com.hanserwei.community.entity.request.house.HouseAddRequest;
import com.hanserwei.community.entity.request.house.HouseQueryRequest;
import com.hanserwei.community.entity.vo.HouseVO;
import com.hanserwei.community.enums.ResponseCodeEnum;
import com.hanserwei.community.service.HouseService;
import com.hanserwei.community.uitils.ThrowUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/house")
@RequiredArgsConstructor
public class HouseController {

    private final HouseService houseService;

    @PostMapping("/search")
    @Operation(summary = "条件查询房屋列表")
    public ApiResponse<Page<HouseVO>> queryHouseByPage(@RequestBody HouseQueryRequest request) {
        Page<HouseVO> result = houseService.queryHouseByPage(request);
        return ApiResponse.success("查询成功！", result);
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询房屋列表")
    public ApiResponse<Page<HouseVO>> listHouse(
            @Parameter(description = "当前页码，例如：1") @RequestParam("currentNumber") Integer currentNumber,
            @Parameter(description = "每页显示数量，例如：10") @RequestParam("pageSize") Integer pageSize
    ) {
        Page<HouseVO> result = houseService.listByPage(currentNumber, pageSize);
        return ApiResponse.success("查询成功！", result);
    }

    @GetMapping("/building/{buildingId}")
    @Operation(summary = "根据建筑物ID查询房屋列表")
    public ApiResponse<List<HouseVO>> getHousesByBuildingId(
            @Parameter(description = "建筑物ID") @PathVariable Integer buildingId
    ) {
        List<HouseVO> result = houseService.getHousesByBuildingId(buildingId);
        return ApiResponse.success("查询成功！", result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询房屋详情")
    public ApiResponse<HouseVO> getHouseById(
            @Parameter(description = "房屋ID") @PathVariable Integer id
    ) {
        ThrowUtils.throwIf(id == null || id <= 0, ResponseCodeEnum.INVALID_PARAMETER, "房屋ID参数错误！");
        House house = houseService.getById(id);
        ThrowUtils.throwIf(house == null, ResponseCodeEnum.RESOURCE_NOT_FOUND, "房屋不存在！");
        HouseVO result = houseService.getHouseVO(house);
        return ApiResponse.success("查询成功！", result);
    }

    @PostMapping("/add")
    @Operation(summary = "添加房屋")
    public ApiResponse<Boolean> addHouse(@RequestBody HouseAddRequest request) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        boolean result = houseService.addHouse(request);
        return ApiResponse.success("添加成功！", result);
    }

    @PostMapping("/update/{id}")
    @Operation(summary = "更新房屋信息")
    public ApiResponse<Boolean> updateHouse(
            @RequestBody HouseAddRequest request,
            @PathVariable Integer id
    ) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        boolean result = houseService.updateHouse(request, id);
        return ApiResponse.success("更新成功！", result);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除房屋")
    public ApiResponse<Boolean> deleteHouse(
            @Parameter(description = "房屋ID") @RequestParam("id") Integer id
    ) {
        boolean result = houseService.removeById(id);
        ThrowUtils.throwIf(!result, ResponseCodeEnum.SYSTEM_ERROR, "删除失败！");
        return ApiResponse.success("删除成功！", true);
    }

    @DeleteMapping("/deleteBatch")
    @Operation(summary = "批量删除房屋")
    public ApiResponse<Boolean> deleteHouseBatch(
            @Parameter(description = "房屋ID列表") @RequestBody Integer[] ids
    ) {
        ThrowUtils.throwIf(ids == null || ids.length == 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        boolean result = houseService.removeBatchByIds(List.of(ids));
        ThrowUtils.throwIf(!result, ResponseCodeEnum.SYSTEM_ERROR, "删除失败！");
        return ApiResponse.success("删除成功！", true);
    }
}
